package com.hxc.shop.service;

import com.hxc.shop.bean.ShopGoodsBuyRecord;
import com.hxc.shop.bean.ShopGoodsInfo;
import com.hxc.shop.bean.ShopGoodsOrder;
import com.hxc.shop.bean.ShopUserInfo;
import com.hxc.shop.bean.Vo.GoodsOrderVo;
import com.hxc.shop.common.ResultCode;
import com.hxc.shop.common.ResultVO;
import com.hxc.shop.mapper.Mysql.GoodsInfoMapper;
import com.hxc.shop.mapper.Mysql.GoodsOrderMapper;
import com.hxc.shop.mapper.Mysql.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GoodsOrderService {
    @Resource
    private GoodsOrderMapper goodsOrderMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private GoodsInfoMapper goodsInfoMapper;

    public ResultVO addGoodsOrder(ShopGoodsOrder order) {
        if (order == null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品订单数据为空，新增订单失败!");
        }
        if (StringUtils.isEmpty(order.getUserName())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"用户名为空,新增订单失败!");
        }
        if (StringUtils.isEmpty(order.getGoodsId()) || order.getGoodsId() == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"商品编号为空，新增订单失败!");
        }
        if (StringUtils.isEmpty(order.getGoodsBuyCount())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"购买商品的数量不能为空!");
        }
        if (StringUtils.isEmpty(order.getGoodsBuyAllPrice())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"购买金额不能为空!");
        }
        if (StringUtils.isEmpty(order.getGoodsAddress())) {
            return new ResultVO(ResultCode.ERROR.getCode(),"购物地址不能为空!");
        }
        // 检查用户名
        String checkUserName = "";
        try {
            checkUserName = userMapper.findUserNameByUserName(order.getUserName());
        }catch (Exception e) {
            log.error("新增商品订单获取用户失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户信息失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户,新增商品订单失败!");
        }
        Integer checkGoodsId = 0;
        try {
            checkGoodsId = goodsInfoMapper.findGoodsIdByGoodsId(order.getGoodsId());
        }catch (Exception e) {
            log.error("新增商品订单获取商品编号失败:"+order.getGoodsId()+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找商品编号失败:"+e.getMessage());
        }
        if (checkGoodsId == null || checkGoodsId == 0) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品编号,新增商品订单失败!");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        // 获取用户信息
        ShopUserInfo shopUserInfo = new ShopUserInfo();
        try {
            shopUserInfo = userMapper.getUserInfoByUserName(order.getUserName());
        }catch (Exception e) {
            log.error("查找用户记录失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户失败:"+e.getMessage());
        }
        // 查找用户的余额是否大于购买的商品总价
        if (Double.valueOf(order.getGoodsBuyAllPrice()) > shopUserInfo.getBlance()) {
            return new ResultVO(ResultCode.ERROR.getCode(),"余额不足，购买商品失败!");
        }
        // 用户剩余余额
        Double leaveBlance = shopUserInfo.getBlance() - Double.valueOf(order.getGoodsBuyAllPrice());
        // 更新用户余额
        try {
            userMapper.updateUserBlance(shopUserInfo.getUserName(),leaveBlance,sdf.format(new Date()));
        }catch (Exception e) {
            log.error("更新用户余额失败:"+shopUserInfo+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"更新用户余额失败:"+e.getMessage());
        }

        // 更新商品库存剩余数量
        ShopGoodsInfo goodsInfo = new ShopGoodsInfo();
        try {
            goodsInfo = goodsInfoMapper.getGoodsById(order.getGoodsId());
        }catch (Exception e) {
            log.error("查找商品信息失败:"+e.getMessage());
        }
        if (goodsInfo!=null) {
            if (Integer.valueOf(order.getGoodsBuyCount()) > goodsInfo.getGoodsInventoryCount()) {
                userMapper.updateUserBlance(shopUserInfo.getUserName(),shopUserInfo.getBlance(),sdf.format(new Date()));
                return new ResultVO(ResultCode.ERROR.getCode(),"商品库存不足，购买失败!");
            } else {
                Integer leaveGoodsInventoryCount = goodsInfo.getGoodsInventoryCount() - Integer.valueOf(order.getGoodsBuyCount());
                goodsInfo.setGoodsInventoryCount(leaveGoodsInventoryCount);
                goodsInfo.setUpdateTime(sdf.format(new Date()));
                try {
                    goodsInfoMapper.updateGoods(goodsInfo);
                }catch (Exception e) {
                    log.error("更新商品库存失败:"+e.getMessage());
                    userMapper.updateUserBlance(shopUserInfo.getUserName(),shopUserInfo.getBlance(),sdf.format(new Date()));
                    return new ResultVO(ResultCode.ERROR.getCode(),"更新商品库存失败:"+e.getMessage());
                }
            }
        } else {
            userMapper.updateUserBlance(shopUserInfo.getUserName(),shopUserInfo.getBlance(),sdf.format(new Date()));
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此商品数据，购买失败!");
        }

        // 新增订单
        order.setCreateTime(sdf.format(new Date()));
        order.setUpdateTime(sdf.format(new Date()));
        order.setGoodsStatus("2");
        try {
            goodsOrderMapper.insertGoodsOrder(order);
        }catch (Exception e) {
            log.error("新增订单失败:"+order+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"新增订单失败:"+e.getMessage());
        }
        // 添加到购买记录表
        ShopGoodsBuyRecord shopGoodsBuyRecord = new ShopGoodsBuyRecord();
        shopGoodsBuyRecord.setCreateTime(sdf.format(new Date()));
        shopGoodsBuyRecord.setUpdateTime(sdf.format(new Date()));
        shopGoodsBuyRecord.setGoodsBuyAllPrice(order.getGoodsBuyAllPrice());
        shopGoodsBuyRecord.setGoodsBuyCount(order.getGoodsBuyCount());
        shopGoodsBuyRecord.setGoodsBuyPrice(order.getGoodsBuyPrice());
        shopGoodsBuyRecord.setGoodsId(String.valueOf(order.getGoodsId()));
        shopGoodsBuyRecord.setUserName(order.getUserName());
        shopGoodsBuyRecord.setGoodsAddress(order.getGoodsAddress());
        try {
            goodsOrderMapper.insertGoodsBuyRecord(shopGoodsBuyRecord);
        }catch (Exception e) {
            log.error("添加到购买记录表失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS.getCode(),"新增订单成功!");
    }

    public ResultVO getAllGoodsOrder(String userName) {
        if (StringUtils.isEmpty(userName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"用户名为空，获取订单数据失败!");
        }
        // 检查用户名
        String checkUserName = "";
        try {
            checkUserName = userMapper.findUserNameByUserName(userName);
        }catch (Exception e) {
            log.error("获取所有商品订单获取用户失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户信息失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户,获取商品订单失败!");
        }
        List<ShopGoodsOrder> goodsOrderList = new ArrayList<>();
        try {
            goodsOrderList = goodsOrderMapper.getAllGoodsOrder(userName);
        }catch (Exception e) {
            log.error("获取订单数据失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取订单数据失败:"+e.getMessage());
        }

        List<GoodsOrderVo> goodsOrderVoList = new ArrayList<>();
        // 处理商品数据
        if (goodsOrderList.size()!=0) {
            for (ShopGoodsOrder order:goodsOrderList) {
                GoodsOrderVo goodsOrderVo = new GoodsOrderVo();
                goodsOrderVo.setId(order.getId());
                goodsOrderVo.setCreateTime(order.getCreateTime());
                goodsOrderVo.setUpdateTime(order.getUpdateTime());
                goodsOrderVo.setGoodsAddress(order.getGoodsAddress());
                goodsOrderVo.setGoodsId(order.getGoodsId());
                goodsOrderVo.setGoodsBuyAllPrice(order.getGoodsBuyAllPrice());
                goodsOrderVo.setGoodsBuyPrice(order.getGoodsBuyPrice());
                goodsOrderVo.setGoodsBuyCount(order.getGoodsBuyCount());
                goodsOrderVo.setGoodsStatus(order.getGoodsStatus());
                goodsOrderVo.setUserName(order.getUserName());
                // 根据商品id获取商品信息
                ShopGoodsInfo shopGoodsInfo = new ShopGoodsInfo();
                try{
                    shopGoodsInfo = goodsInfoMapper.getGoodsById(order.getGoodsId());
                }catch (Exception e) {
                    log.error("获取商品信息失败:"+order.getGoodsId()+"=="+e.getMessage());
                }
                if (shopGoodsInfo!=null) {
                    goodsOrderVo.setGoodsName(shopGoodsInfo.getGoodsName());
                    goodsOrderVo.setGoodsPrice(shopGoodsInfo.getGoodsPrice());
                    goodsOrderVo.setGoodsImage(shopGoodsInfo.getGoodsImage());
                }
                goodsOrderVoList.add(goodsOrderVo);
            }
        }
        return new ResultVO(ResultCode.SUCCESS,goodsOrderVoList);
    }


    public ResultVO getGoodsOrderByStatus(String userName,String status) {
        if (StringUtils.isEmpty(userName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"用户名为空，获取订单数据失败!");
        }
        if (StringUtils.isEmpty(status)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"订单状态为空，获取订单数据失败!");
        }
        // 检查用户名
        String checkUserName = "";
        try {
            checkUserName = userMapper.findUserNameByUserName(userName);
        }catch (Exception e) {
            log.error("获取商品订单获取用户失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户信息失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户,获取商品订单失败!");
        }
        List<ShopGoodsOrder> goodsOrderArrayList = new ArrayList<>();
        try {
            goodsOrderArrayList = goodsOrderMapper.getGoodsOrderByStatus(userName,status);
        }catch (Exception e) {
            log.error("获取订单数据失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取订单数据失败:"+e.getMessage());
        }

        List<GoodsOrderVo> goodsOrderVoArrayList = new ArrayList<>();
        // 处理商品数据
        if (goodsOrderArrayList.size()!=0) {
            for (ShopGoodsOrder goodsOrder:goodsOrderArrayList) {
                GoodsOrderVo goodsOrderVo = new GoodsOrderVo();
                goodsOrderVo.setId(goodsOrder.getId());
                goodsOrderVo.setGoodsBuyAllPrice(goodsOrder.getGoodsBuyAllPrice());
                goodsOrderVo.setGoodsBuyPrice(goodsOrder.getGoodsBuyPrice());
                goodsOrderVo.setGoodsBuyCount(goodsOrder.getGoodsBuyCount());
                goodsOrderVo.setGoodsStatus(goodsOrder.getGoodsStatus());
                goodsOrderVo.setUserName(goodsOrder.getUserName());
                goodsOrderVo.setCreateTime(goodsOrder.getCreateTime());
                goodsOrderVo.setUpdateTime(goodsOrder.getUpdateTime());
                goodsOrderVo.setGoodsAddress(goodsOrder.getGoodsAddress());
                goodsOrderVo.setGoodsId(goodsOrder.getGoodsId());
                // 根据商品id获取商品信息
                ShopGoodsInfo shopGoodsInfo = new ShopGoodsInfo();
                try{
                    shopGoodsInfo = goodsInfoMapper.getGoodsById(goodsOrder.getGoodsId());
                }catch (Exception e) {
                    log.error("获取商品信息失败:"+goodsOrder.getGoodsId()+"=="+e.getMessage());
                }

                if (shopGoodsInfo!=null) {
                    goodsOrderVo.setGoodsPrice(shopGoodsInfo.getGoodsPrice());
                    goodsOrderVo.setGoodsImage(shopGoodsInfo.getGoodsImage());
                    goodsOrderVo.setGoodsName(shopGoodsInfo.getGoodsName());
                }
                goodsOrderVoArrayList.add(goodsOrderVo);
            }
        }
        return new ResultVO(ResultCode.SUCCESS, goodsOrderVoArrayList);
    }

    public ResultVO getAllGoodsBuyRecords() {
        List<ShopGoodsBuyRecord> shopGoodsBuyRecordList = new ArrayList<>();
        try {
            shopGoodsBuyRecordList = goodsOrderMapper.getAllBuyRecord();
        }catch (Exception e) {
            log.error("获取所有购买记录失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取所有购买记录失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,shopGoodsBuyRecordList);
    }

    public ResultVO getGoodsBuyRecordsByUserName(String userName) {
        if (StringUtils.isEmpty(userName)) {
            return new ResultVO(ResultCode.ERROR.getCode(), "用户名为空,获取人员购买记录失败!");
        }
        // 检查用户名
        String checkUserName = "";
        try {
            checkUserName = userMapper.findUserNameByUserName(userName);
        }catch (Exception e) {
            log.error("获取商品购买记录获取用户失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户信息失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户,获取商品购买记录失败!");
        }
        List<ShopGoodsBuyRecord> shopGoodsBuyRecordList = new ArrayList<>();
        try {
            shopGoodsBuyRecordList = goodsOrderMapper.getBuyRecordByUserName(userName);
        }catch (Exception e) {
            log.error("获取人员购买记录失败:"+userName+"=="+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"获取人员购买记录失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS,shopGoodsBuyRecordList);
    }

    public ResultVO deleteGoodsBuyRecordsByUserNameAndBuyRecordId(String userName,Integer id) {
        if (StringUtils.isEmpty(userName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"未获取到用户名！");
        }
        if (id == null || id == 0) {
            return  new ResultVO(ResultCode.ERROR.getCode(),"未获取到购买记录编号:"+id);
        }
        // 检查用户名
        String checkUserName = "";
        try {
            checkUserName = userMapper.findUserNameByUserName(userName);
        }catch (Exception e) {
            log.error("获取商品购买记录获取用户失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户信息失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户,获取商品购买记录失败!");
        }
        ShopGoodsBuyRecord shopGoodsBuyRecord = new ShopGoodsBuyRecord();
        try {
            shopGoodsBuyRecord = goodsOrderMapper.GoodsBuyRecordsByUserNameAndBuyRecordId(userName,id);
        }catch (Exception e) {
            return new ResultVO(ResultCode.ERROR.getCode(),"获取购买记录失败:"+e.getMessage());
        }
        if (shopGoodsBuyRecord==null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无购买记录!");
        }
        try {
            goodsOrderMapper.deleteGoodsOrderByUserNameAndId(userName,id);
        }catch (Exception e){
            return new ResultVO(ResultCode.ERROR.getCode(),"删除失败:"+e.getMessage());
        }
        return new ResultVO(ResultCode.SUCCESS.getCode(),"删除成功!");
    }

    public ResultVO getGoodsBuyRecordsByUserNameAndBuyRecordId(String userName,Integer id) {
        if (id == null || id == 0) {
            return  new ResultVO(ResultCode.ERROR.getCode(),"未获取到购买记录编号:"+id);
        }
        if (StringUtils.isEmpty(userName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"未获取到用户名！");
        }
        // 检查用户名
        String checkUserName = "";
        try {
            checkUserName = userMapper.findUserNameByUserName(userName);
        }catch (Exception e) {
            log.error("获取商品购买记录获取用户失败:"+e.getMessage());
            return new ResultVO(ResultCode.ERROR.getCode(),"查找用户信息失败:"+e.getMessage());
        }
        if (StringUtils.isEmpty(checkUserName)) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无此用户,获取商品购买记录失败!");
        }
        ShopGoodsBuyRecord shopGoodsBuyRecord = new ShopGoodsBuyRecord();
        try {
            shopGoodsBuyRecord = goodsOrderMapper.GoodsBuyRecordsByUserNameAndBuyRecordId(userName,id);
        }catch (Exception e) {
            return new ResultVO(ResultCode.ERROR.getCode(),"获取购买记录失败:"+e.getMessage());
        }
        if (shopGoodsBuyRecord==null) {
            return new ResultVO(ResultCode.ERROR.getCode(),"查无购买记录!");
        }
        return new ResultVO(ResultCode.SUCCESS,shopGoodsBuyRecord);
    }


   public ResultVO updateGoodsOrderById(Integer id,String goodsStatus) {
        if (StringUtils.isEmpty(goodsStatus)) {
            return  new ResultVO(ResultCode.ERROR.getCode(),"未获取到订单状态!");
        }
       if (id == null || id == 0) {
           return  new ResultVO(ResultCode.ERROR.getCode(),"未获取到购买记录编号!");
       }
       ShopGoodsBuyRecord shopGoodsBuyRecord = goodsOrderMapper.findGoodsOrderById(id);
       if (shopGoodsBuyRecord == null) {
           return  new ResultVO(ResultCode.ERROR.getCode(),"查无此订单编号:"+id);
       }
       try {
           goodsOrderMapper.updateGoodsOrderStatusById(id,goodsStatus);
       }catch (Exception e) {
           return  new ResultVO(ResultCode.ERROR.getCode(),"更新商品状态失败:"+id+","+e.getMessage());
       }
       return  new ResultVO(ResultCode.ERROR.getCode(),"更新商品状态成功:"+id);
   }

   public ResultVO getGoodsOrderById(Integer id) {
       if (id == null || id == 0) {
           return  new ResultVO(ResultCode.ERROR.getCode(),"未获取到购买记录编号!");
       }
       ShopGoodsBuyRecord shopGoodsBuyRecord = goodsOrderMapper.findGoodsOrderById(id);
       if (shopGoodsBuyRecord == null) {
           return  new ResultVO(ResultCode.ERROR.getCode(),"查无此订单编号:"+id);
       }
      return new ResultVO(ResultCode.SUCCESS,shopGoodsBuyRecord);
   }
}
